package com.anbinh.doctor.fragment.account;

import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.model.ChildrenComment;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.Doctor;
import com.anbinh.doctor.model.ResponseDetaitlComment;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.zcw.togglebutton.ToggleButton;
import com.zcw.togglebutton.ToggleButton.OnToggleChanged;

public class FragmentCommentDetailt extends Fragment {
	private static FragmentCommentDetailt fragment = null;
	private View mView;
	private LinearLayout lineComment;
	private UserLogin userLogin;
	private CircleProgressBar loadding;

	private TextView lblName;
	private TextView lblTime;
	private TextView lblContent;
	private ImageView imgAva;
	private ImageLoader imageLoader;

	private RatingBar rateCamnhanchung;
	private RatingBar rateThoigiancho;
	private RatingBar rateCosovatchat;
	private EditText edContent;
	private ResponseDetaitlComment currentCommnet;

	public static FragmentCommentDetailt newInstance() {
		if (fragment == null) {
			fragment = new FragmentCommentDetailt();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()
				.getApplicationContext()));
		super.onAttach(activity);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_comment_detailt, null);
		Utils.setOnTouchHideKeyboard(getActivity(), mView);

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		lineComment = (LinearLayout) mView.findViewById(R.id.lineComment);
		loadding = (CircleProgressBar) mView.findViewById(R.id.loadding);
		lblName = (TextView) mView.findViewById(R.id.lblName);
		lblTime = (TextView) mView.findViewById(R.id.lblTime);
		lblContent = (TextView) mView.findViewById(R.id.lblContent);
		imgAva = (ImageView) mView.findViewById(R.id.icon);
		edContent = (EditText) mView.findViewById(R.id.edContent);
		rateCamnhanchung = (RatingBar) mView
				.findViewById(R.id.rateCamnhanchung);
		rateThoigiancho = (RatingBar) mView.findViewById(R.id.rateThoigiancho);
		rateCosovatchat = (RatingBar) mView.findViewById(R.id.rateCosovatchat);
		onLoadCommentDetailt();

		Button btnTraloi = (Button) mView.findViewById(R.id.btnTraloi);
		btnTraloi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String mComment = edContent.getText().toString().trim();
				if (mComment.isEmpty()) {
					Utils.showMessage(
							getActivity(),
							getResources().getString(
									R.string.vui_long_nhap_noi_dung));
					return;
				}
				ChildrenComment childrenComment = new ChildrenComment();
				Doctor doctor = new Doctor();
				doctor.setName(userLogin.getData().getUserInfo().getFull_name());
				doctor.setAvatar(userLogin.getData().getUserInfo().getAvatar());
				childrenComment.setDoctor(doctor);
				childrenComment.setIsActive(1);
				childrenComment.setComment(mComment);
				childrenComment.setUpdatedAt(Utils
						.getCurrentDayName("dd/MM/yyyy"));
				lineComment.addView(addItem(childrenComment));
				onPostComment(mComment);

			}
		});

	}

	public void onPostComment(final String comment) {
		Utils.disableEnableControls(false, mView);

		ServerController.getInstance(getActivity()).requestReaplyComment(
				userLogin.getData().getToken(), comment,
				currentCommnet.getData().getId(),
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						loadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
					}

					@Override
					public void onError(int errorCode, String message) {
						Utils.disableEnableControls(true, mView);

					}
				});
	}

	public void onLoadCommentDetailt() {
		loadding.setVisibility(View.VISIBLE);
		ServerController.getInstance(getActivity()).requestDetailtComment(
				userLogin.getData().getToken(),
				FragmentListComment.newInstance().getCurrentComment().getId(),
				new OnRequest<ResponseDetaitlComment>() {

					@Override
					public void onSuccess(ResponseDetaitlComment data) {
						loadding.setVisibility(View.INVISIBLE);
						if (getActivity() == null)
							return;
						lineComment.removeAllViews();

						if (data.getResponsecode() >= 0) {

							currentCommnet = data;
							Bitmap bmp = imageLoader.loadImageSync(data
									.getData().getPatient().getAvatar());
							if (bmp == null) {

								imageLoader.displayImage(data.getData()
										.getPatient().getAvatar(), imgAva,
										Utils.getOptions(getActivity()),
										new SimpleImageLoadingListener() {
											@Override
											public void onLoadingStarted(
													String imageUri, View view) {
											}

											@Override
											public void onLoadingFailed(
													String imageUri, View view,
													FailReason failReason) {
											}

											@Override
											public void onLoadingComplete(
													String imageUri, View view,
													Bitmap loadedImage) {
											}
										});
							} else {
								imgAva.setImageBitmap(bmp);
							}

							rateThoigiancho.setRating(data.getData()
									.getRating().getWaiting());
							rateCamnhanchung.setRating(data.getData()
									.getRating().getCommonSense());
							rateCosovatchat.setRating(data.getData()
									.getRating().getFacilities());

							lblName.setText(data.getData().patient.getName());
							lblTime.setText(data.getData().getCreatedAt());
							lblContent
									.setText(data.getData().getShortComment());

							List<ChildrenComment> mList = data.getData()
									.getChildren();
							if (mList != null && mList.size() > 0) {
								for (int i = 0; i < mList.size(); i++) {
									lineComment.addView(addItem(mList.get(i)));
								}
							}
						}

					}

					@Override
					public void onError(int errorCode, String message) {
						loadding.setVisibility(View.INVISIBLE);
					}
				});

	}

	public View addItem(final ChildrenComment item) {
		View view = getActivity().getLayoutInflater().inflate(
				R.layout.item_comment_doctor, null);
		lblName = (TextView) view.findViewById(R.id.lblName);
		lblTime = (TextView) view.findViewById(R.id.lblTime);
		lblContent = (TextView) view.findViewById(R.id.lblContent);
		lblName.setText(item.getDoctor().getName());
		lblTime.setText(item.getUpdatedAt());
		lblContent.setText(item.getComment());
		ToggleButton toggleButton = (ToggleButton) view
				.findViewById(R.id.btnToggle);
		if (item.getIsActive() == 1) {
			toggleButton.setToggleOn(true);
		} else {
			toggleButton.setToggleOn(false);
		}

		toggleButton.setOnToggleChanged(new OnToggleChanged() {

			@Override
			public void onToggle(boolean on) {
				onOff(item.getId());
			}
		});

		ImageView imgAva = (ImageView) view.findViewById(R.id.icon);

		Bitmap bmp1 = imageLoader.loadImageSync(item.getDoctor().getAvatar());
		if (bmp1 == null) {

			imageLoader.displayImage(item.getDoctor().getAvatar(), imgAva,
					Utils.getOptions(getActivity()),
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingStarted(String imageUri, View view) {
						}

						@Override
						public void onLoadingFailed(String imageUri, View view,
								FailReason failReason) {
						}

						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap loadedImage) {
						}
					});
		} else {
			imgAva.setImageBitmap(bmp1);
		}
		return view;
	}

	public void onOff(int id) {
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);

		ServerController.getInstance(getActivity()).requestOnoffComment(
				userLogin.getData().getToken(), id,
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {

					}

					@Override
					public void onError(int errorCode, String message) {

					}
				});
	}
}
